package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.admin.dto.sys.SubWayDto;
import com.yun.zf.common.admin.model.house.SearchMo;
import com.yun.zf.common.app.domain.ZfSearch;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.common.utils.TreeNodes;
import com.yun.zf.system.mapper.ZfSearchMapper;
import com.yun.zf.system.service.ZfCityService;
import com.yun.zf.system.service.ZfSearchService;
import com.yun.zf.system.service.ZfSubwayService;
import com.yun.zf.system.service.ZfSubwayStationService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log
public class ZfSearchServiceImpl extends ServiceImpl<ZfSearchMapper, ZfSearch> implements ZfSearchService {

    @Autowired
    private ZfCityService cityService;
    @Autowired
    private ZfSubwayService subwayService;
    @Autowired
    private ZfSubwayStationService stationService;

    /**
     * 获取搜索条件
     *
     * @param type     新房or 二手房
     * @param cityCode 城市代码
     * @return
     */
    public SearchMo getSearchMo(String type, Long cityCode ,String areaOrSubway) {
        SearchMo searchMo = new SearchMo();
        // 获取通用条件
        QueryWrapper<ZfSearch> searchQueryWrapper = new QueryWrapper<>();
        searchQueryWrapper.select(ZfSearch.class, search -> !search.getColumn().equals("create_time") && !search.getColumn().equals("modified_time")&&!search.getColumn().equals("remark")).eq("type", type);
        searchQueryWrapper.and(wrapper -> wrapper.eq("city", -1).or().eq("city", cityCode));
        searchQueryWrapper.orderByAsc("sort");
        List<ZfSearch> searchList = baseMapper.selectList(searchQueryWrapper);
        searchMo.setSearchList(searchList);
        // 如果是根据区域，则查询区域内容
        if (StringUtils.isNotBlank(areaOrSubway)){
            if (areaOrSubway.equals("area")){
                List<TreeNodes> trees  = cityService.getDistrict(cityCode);
                searchMo.setCityList(trees);
            }else if (areaOrSubway.equals("subway")){
                // 查询地铁线路
                List<SubWayDto> subways = subwayService.subwaysByCity(cityCode);
                for (SubWayDto way:subways
                ) {
                    way.setStationDtos(stationService.subwayStations(Long.parseLong(way.getId())));
                }
                searchMo.setSubWatList(subways);
            }
        }


        return searchMo;
    }


}
